[mypyc] fix IntEnum attributes not treated as final values#14745
[mypyc] fix IntEnum attributes not treated as final values#14745elbehery95 wants to merge 3 commits into
Conversation
|
As per my understanding this seems to be an issue in cpython implementation that was fixed in later releases, but could not be backported to 3.8 as it was back then allowing security fixes only. |
ichard26
left a comment
There was a problem hiding this comment.
Hey thanks for filing a PR. I'm a new reviewer so this in no way counts as a full review, but here's some initial feedback:
- Typically we prefix commits and PRs focused on mypyc with
[mypyc]. For example ... - Could you test the function based syntax for IntEnum?
Color = IntEnum("Color", ["GREEN"])
- Add
Fixes mypyc/mypyc#721somewhere in your PR description so GitHub will automatically close the related bug report once this PR is merged.
IntEnum test is skipped on python3.8 due to python/cpython#26654
899c1d2 to
3779501
Compare
|
Thanks so much @ichard26 Regarding the functional based syntax, I think it is not working correctly for both import enum
FunctionalColor = enum.Enum("FunctionalColor", ["RED", "BLUE"])
assert FunctionalColor.BLUE == 2Probably would require a separate patch |
ichard26
left a comment
There was a problem hiding this comment.
Looks good except from the tests.
It's probably a good idea to also test Flag enums, although I'm not sure whether mypyc still supports Python 3.5 as a compilation target. You may have to version guard this one too.
StrEnum and IntFlag would be nice to test too, but those are new in Python 3.11 so I won't complain if you don't add them in.
add reference to bpo associated with skipping test on python 3.8
|
Thanks so much @ichard26 for the feedback, I have added fix for And after trying to understand test structure more, I have added tests for Should title change to reflect `StrEnum fix as well?
E.g. from enum import StrEnum
class ColorFlag(StrEnum):
RED = "red"Reports
class ColorFlag(Flag):
RED = auto()
GREEN = auto()
BLUE = auto()
purple = ColorFlag.RED | ColorFlag.BLUE
assert ColorFlag.RED in purpleReports
|
add tests for StrEnum, Flag and IntFlag
| @@ -467,12 +467,13 @@ def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value: | |||
| The tuple is passed to the metaclass constructor. | |||
There was a problem hiding this comment.
should docstring change in this case ?
| if cls.fullname == "builtins.object": | ||
| continue | ||
| if is_named_tuple and cls.fullname in ( | ||
| if (is_named_tuple or is_enum) and cls.fullname in ( |
There was a problem hiding this comment.
My thoughts about this change are as the following:
is_extension_classwill return False forStrEnumasmetaclass_typeis set toenum.EnumMetaLine 130 in 284142d
transform_class_defmentioned explicitly in comments that classes inheriting fromenumwill have non-extension class object created for themmypy/mypyc/irbuild/classdef.py
Line 107 in 284142d
- For some reason
populate_non_ext_basesis explicitly checking for named tuple though any class inheriting saystror have atyping.Sequencein mro it will cause the error currently seen in building code withStrEnummypy/mypyc/irbuild/classdef.py
Line 475 in 284142d
Not really sure if checking against both Enums and Named tuples is generic enough or not.
|
Hey, I know that I've been away for too long, but you don't need to close your PR. Unfortunately, PR reviews are scarce around here. Things don't always move quickly or even at a decent pace. I'm sorry about all of this, your work is appreciated. You're welcome to reopen this, or if you'd prefer it, I'm happy to file a new PR with your commits (attributed to you only!) and try to get it landed eventually. |
|
@ichard26 Did you end up reviving this PR? |
|
Nope. I've also stepped back from contributing to mypyc as reviews were few and far between. Sorry. |
Fixes mypyc/mypyc#721
Generated
__native.cwill not compile when mypyc is invoked against the followingexample.py:Error
Previously code only treated
enum.Enumattributes as Final, I switched to usingTypeInfo.is_enuminstead of checking type full name against hard-coded stringYour feedback is appreciated